<script setup>
import {ref, watch} from "vue";
import {friendsListGetAPI} from "@/api/FrontApi/user.js";

// 接收父组件的 v-model 绑定
const props = defineProps({
  selectId: String,
  isFriendsListShow: Boolean
})

// 发出事件以通知父组件更新 v-model 的值
const emit = defineEmits(['update:isFriendsListShow', 'chooseFriend'])

const friendsList = ref([])

// 监听 isFriendsListShow 的变化
// watch(() => props.isFriendsListShow, (newValue) => {
//   if (newValue) {
//     activateMethod();
//   }
// })

// 点击背景关闭组件
const closeFriendsList = () => {
  emit('update:isFriendsListShow', false);
}

// 选中item
const selectItem = (item) => {
  emit('chooseFriend', item, props.selectId)
  emit('update:isFriendsListShow', false);
}

// 搜索实现
const usernameOrNickname = ref()
const timer = ref()
watch(usernameOrNickname, (newValue) => {
  friendsList.value = []
  clearTimeout(timer.value)
  timer.value = setTimeout(async () => {
    const res = await friendsListGetAPI(newValue)
    friendsList.value = res.data
  }, 300);
}, { deep: true, immediate: true });
</script>

<template>
  <div v-show="isFriendsListShow" class="overlay" @click="closeFriendsList">
    <div class="friends-list" @click.stop>
      <div class="title">选择礼物接收人</div>
      <div class="search">
        <input v-model="usernameOrNickname" placeholder="根据用户名或者昵称搜索好友">
      </div>
      <div class="list">
        <div @click="selectItem(item)" class="item" v-for="item in friendsList" :key="item">
          <img :src="item.avatar">
          <div>{{item.nickname}}({{item.username}})</div>
        </div>
      </div>
      <i @click="closeFriendsList" class="iconfont icon-cuocha_kuai"></i>
    </div>
  </div>
</template>

<style scoped lang="scss">
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000; /* 确保在最上方 */
  .friends-list {
    width: 500px;
    height: 800px;
    background: radial-gradient(circle at top left, rgba(74, 81, 92, 0.4) 0%, rgba(75, 81, 92, 0) 60%), #25282e;
    padding: 20px 40px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1001; /* 确保在 overlay 之上 */
    position: relative;
    display: flex;
    flex-direction: column;
    .title {
      color: #fff;
      font-size: 22px;
      font-weight: 700;
    }
    .search {
      width: 100%;
      margin: 10px 0;
      input {
        width: 100%;
        background: rgba(59, 63, 72, .5);
        color: #dfe3e6;
        font-size: 14px;
        font-weight: 300;
        border: none;
        border-radius: 3px;
        box-shadow: inset 0px 0px 0px #0000;
        display: block;
        line-height: 22px;
        padding: 10px;
        &:focus {
          border-color: #707070;
          box-shadow: 0 0 5px rgb(14, 14, 14);
          background-color: rgba(41, 46, 56, 0.5);;
          outline: none;
        }
      }
    }
    .list {
      flex: 1;
      overflow-y: auto;
      scroll-padding-top: 20px;
      scroll-padding-bottom: 20px;
      overflow-x: hidden;
      display: flex;
      flex-direction: column;
      gap: 5px;
      .item {
        background: rgba(21, 21, 21, .25);
        border-color: rgba(21, 21, 21, .25);
        border-width: 1px;
        border-style: solid;
        display: flex;
        align-items: center;
        padding: 10px;
        font-size: 13px;
        border-radius: 2px;
        cursor: pointer;
        img {
          margin-right: 10px;
          width: 30px;
          height: 30px;
          box-shadow: 2px 2px 8px 1px rgba(0, 0, 0, .3);
          display: inline-block;
        }
        div {
          color: #a0a0a0;
        }
        &:hover {
          background: rgba(21, 21, 21, .15);
          border-color: rgba(21, 21, 21, .15);
        }
      }
    }
    i {
      position: absolute;
      top: 0.8%;
      right: 1.6%;
      font-size: 14px;
      color: #cccccc;
      cursor: pointer;
      &:hover {
        color: #fff;
      }
    }
  }
}
</style>
